package com.zenithmind.article.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Min;

/**
 * 标签聚合根
 * 遵循单一职责原则：专注于标签管理
 * 遵循封装原则：通过方法控制状态变更
 * 遵循开闭原则：支持标签类型扩展
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "标签实体", description = "标签聚合根")
public class Tag extends BaseEntity {

    @NotBlank(message = "标签名称不能为空")
    @Size(max = 50, message = "标签名称长度不能超过50个字符")
    @Schema(description = "标签名称")
    private String name;

    @Size(max = 200, message = "标签描述长度不能超过200个字符")
    @Schema(description = "标签描述")
    private String description;

    @Schema(description = "标签颜色")
    private String color;

    @Min(value = 0, message = "使用次数不能为负数")
    @Schema(description = "使用次数")
    private Integer useCount = 0;

    @Schema(description = "标签状态")
    private TagStatus status = TagStatus.ACTIVE;

    @Schema(description = "标签类型")
    private TagType type = TagType.GENERAL;

    /**
     * 标签状态枚举
     */
    public enum TagStatus {
        ACTIVE(1, "启用"),
        DISABLED(0, "禁用");

        private final Integer code;
        private final String description;

        TagStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static TagStatus fromCode(Integer code) {
            if (code == null) return ACTIVE;
            for (TagStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return ACTIVE;
        }
    }

    /**
     * 标签类型枚举
     */
    public enum TagType {
        GENERAL(1, "通用标签"),
        TECHNOLOGY(2, "技术标签"),
        CATEGORY(3, "分类标签"),
        FEATURE(4, "特性标签");

        private final Integer code;
        private final String description;

        TagType(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static TagType fromCode(Integer code) {
            if (code == null) return GENERAL;
            for (TagType type : values()) {
                if (type.code.equals(code)) {
                    return type;
                }
            }
            return GENERAL;
        }
    }

    // === 构造方法 ===

    /**
     * 创建新标签
     */
    public Tag(String name, String description, String color, TagType type) {
        this.name = name;
        this.description = description;
        this.color = color;
        this.type = type != null ? type : TagType.GENERAL;
        this.useCount = 0;
        this.status = TagStatus.ACTIVE;
    }

    /**
     * 创建简单标签
     */
    public Tag(String name) {
        this(name, null, null, TagType.GENERAL);
    }

    // === 业务方法 ===

    /**
     * 启用标签
     */
    public void enable() {
        this.status = TagStatus.ACTIVE;
    }

    /**
     * 禁用标签
     */
    public void disable() {
        this.status = TagStatus.DISABLED;
    }

    /**
     * 增加使用次数
     */
    public void incrementUseCount() {
        this.useCount++;
    }

    /**
     * 减少使用次数
     */
    public void decrementUseCount() {
        if (this.useCount > 0) {
            this.useCount--;
        }
    }

    /**
     * 更新标签信息
     */
    public void updateInfo(String name, String description, String color) {
        this.name = name;
        this.description = description;
        this.color = color;
    }

    /**
     * 设置标签类型
     */
    public void setType(TagType type) {
        this.type = type != null ? type : TagType.GENERAL;
    }

    /**
     * 检查标签是否可用
     */
    public boolean isActive() {
        return this.status == TagStatus.ACTIVE;
    }

    /**
     * 检查是否为热门标签
     */
    public boolean isPopular() {
        return this.useCount >= 10; // 使用次数超过10次认为是热门标签
    }

    /**
     * 检查是否可以删除
     */
    public boolean canDelete() {
        return this.useCount == 0;
    }

    /**
     * 获取状态码（用于数据库存储）
     */
    public Integer getStatusCode() {
        return this.status.getCode();
    }

    /**
     * 设置状态（用于数据库读取）
     */
    public void setStatusCode(Integer statusCode) {
        this.status = TagStatus.fromCode(statusCode);
    }

    /**
     * 获取类型码（用于数据库存储）
     */
    public Integer getTypeCode() {
        return this.type.getCode();
    }

    /**
     * 设置类型（用于数据库读取）
     */
    public void setTypeCode(Integer typeCode) {
        this.type = TagType.fromCode(typeCode);
    }
}
